//
//  ViewController.m
//  01 字体(完整版)
//
//  Created by dllo on 16/2/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"

#import "NHOrderByFirstLetter.h"

#define Width self.view.frame.size.width
#define Height self.view.frame.size.height

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
// 保存原始数据
@property(nonatomic, retain)NSMutableArray *dataArr;
// 排序后的数据
@property(nonatomic, retain)NSMutableArray *orderArr;
// 显示数据
@property(nonatomic, retain)UITableView *tableView;
// 判断是有序的还是无序的
@property(nonatomic, assign)BOOL isOrder;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 初始化判断变量
    self.isOrder = NO;
    
    // 设置navigation
    [self setNavigation];
    // 读取数据
    [self createData];
    // 创建视图
    [self createView];
}
#pragma mark 设置Navigation
- (void)setNavigation{
    self.title = @"汉字按首字母排序";
    // 创建排序按钮
    UIButton *orderButton = [UIButton buttonWithType:UIButtonTypeSystem];
    orderButton.frame = CGRectMake(0, 0, 40, 40);
    [orderButton setTitle:@"排序" forState:UIControlStateNormal];
    [orderButton addTarget:self action:@selector(orderAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:orderButton];
}

#pragma mark 读取数据
- (void)createData{
    
    // 原始数据
    self.dataArr = [NSMutableArray arrayWithArray:[UIFont familyNames]]; //[NH_DataModel baseModelByArr:result[@"data"]];
    
    // 排序
    self.orderArr =  [NHOrderByFirstLetter baseOrderPinYinOnArr:self.dataArr];
    [self.tableView reloadData];
    
}

#pragma mark 创建视图
- (void)createView{
    
    // 创建TableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Width, Height) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
}
#pragma mark 实现排序的点击方法
- (void)orderAction:(UIButton *)orderButton{
    // 无序
    if(self.isOrder == NO){
        [orderButton setTitle:@"无序" forState:UIControlStateNormal];
    }
    // 有序
    else{
        [orderButton setTitle:@"排序" forState:UIControlStateNormal];
    }
    self.isOrder = !self.isOrder;
    [self.tableView reloadData];
}

#pragma mark TableView的协议方法
// 个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // 无序
    if(self.isOrder == NO){
        return self.dataArr.count;
    }
    // 有序
    else{
        return [self.orderArr[section][@"datas"] count];
    }
}
// 设置Section的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    // 无序
    if(self.isOrder == NO){
        return 1;
    }
    // 有序
    else{
        return self.orderArr.count;
    }
}

// 创建Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    // 无序
    if(self.isOrder == NO){
        
        NSString *fontName = self.dataArr[indexPath.row];
        
        cell.textLabel.font = [UIFont fontWithName:fontName size:14.0f];
        cell.textLabel.text= [NSString stringWithFormat:@"%ld、 北京30°ABCabc - %@",indexPath.row,fontName];
    }
    // 有序
    else{
        NSArray *datasArr = self.orderArr[indexPath.section][@"datas"];
        
        NSString *fontName = datasArr[indexPath.row];
        
        cell.textLabel.font = [UIFont fontWithName:fontName size:14.0f];
        cell.textLabel.text= [NSString stringWithFormat:@"北京30°ABCabc - %@",fontName];
    }
    return cell;
}

// 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 无序情况下
    if(self.isOrder == NO){
        NSLog(@"%@",self.dataArr[indexPath.row]);
    }
    // 有序
    else{
        NSArray *datasArr = self.orderArr[indexPath.section][@"datas"];
        NSString *fontName = datasArr[indexPath.row];
        
        NSLog(@"%@",fontName);
    }
}

// 设置分区悬浮标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    // 无序
    if(self.isOrder == NO){
        return nil;
    }
    // 有序
    else{
        return self.orderArr[section][@"title"];
    }
}
// 设置分区快捷条
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    // 无序
    if(self.isOrder == NO){
        return nil;
    }
    // 有序
    else{
        NSMutableArray *titleArr = [NSMutableArray array];
        for (NSDictionary *dic in self.orderArr) {
            [titleArr addObject:dic[@"title"]];
        }
        return titleArr;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
